<?php

$db = mysql_connect('localhost', 'revealweb', 'r3v34ls3cr3t') or die("Can't connect to db");
mysql_select_db('reveal_titles');

if (isset($_GET['c'])) {
  $categoryString = '= ' . (int)$_GET['c'];
} else {
  $categoryString = 'is null';
}

$query = "select * from categories where parent_category_id $categoryString order by category_name";

$results = mysql_query($query);

header('Content-type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<listing>\n";

if (mysql_num_rows($results) > 0) {
  while ($result = mysql_fetch_assoc($results)) {
    //echo '<a href="' . $_SERVER['PHP_SELF'] . '?c=' . $result['category_id'] . '">' . $result['category_name'] . '</a><br />' . "\n";
    echo '<category id="' . (int)$result['category_id'] . '"><![CDATA[' . $result['category_name'] . ']]></category>' . "\n";
  }
} else {
  $titles = mysql_query("select t.* from titles t, title_category tg where t.title_id = tg.title_id and tg.category_id = " . (int)$_GET['c'] . " order by t.title_name");

  if (mysql_num_rows($titles) > 0) {
    while ($title = mysql_fetch_assoc($titles)) {
      //echo '<a href="' . $title['title_url'] . '">' . $title['title_name'] . '</a><br />' . "\n";
      echo '<book>';
      foreach ($title as $field => $value) {
        if ($value != '') {
          echo "<$field><![CDATA[$value]]></$field>";
        }
      }
      echo "</book>\n";
    }
  }
}

echo '</listing>';
?>
